package com.gx.smart.building;

import com.casic.gx.grpc.common.ComQuery;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.AssetDetailApiGrpc;
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.RecordDetailReq;
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.RecordDetailResp;
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.RecordQueryReq;
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.RecordQueryResp;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author: xiaosy
 * 时间: 2022/4/13
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class AssetDetailApiService {
    private String host = BuildConfig.JOYLIFE_GRPC_UAA_SERIVER_URL;
    private String port = "80";

    private static class SingleTonHolder {
        private static final AssetDetailApiService INSTANCE = new AssetDetailApiService();
    }

    public static AssetDetailApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    private AssetDetailApiService() {

    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private AssetDetailApiGrpc.AssetDetailApiBlockingStub getStub(ManagedChannel channel) {
        return AssetDetailApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 查询巡检记录
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, RecordQueryResp> queryInspectionRecordList(
            ComQuery comQuery,
            String assetId,
            CallBack<RecordQueryResp> callBack) {
        return new GrpcAsyncTask<String, Void, RecordQueryResp>(callBack) {
            @Override
            protected RecordQueryResp doRequestData(ManagedChannel channel) {
                RecordQueryReq message = RecordQueryReq.newBuilder()
                        .setComQuery(comQuery)
                        .setTenantNo(1)
                        .setAssetId(assetId)
                        .build();
                Logger.d(message);
                RecordQueryResp response = null;
                try {
                    response = getStub(channel).queryInspectionRecordList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryInspectionRecordList");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 查询保养记录
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, RecordQueryResp> queryMaintenanceRecordList(
            ComQuery comQuery,
            String assetId,
            CallBack<RecordQueryResp> callBack) {
        return new GrpcAsyncTask<String, Void, RecordQueryResp>(callBack) {
            @Override
            protected RecordQueryResp doRequestData(ManagedChannel channel) {
                RecordQueryReq message = RecordQueryReq.newBuilder()
                        .setComQuery(comQuery)
                        .setTenantNo(1)
                        .setAssetId(assetId)
                        .build();
                Logger.d(message);
                RecordQueryResp response = null;
                try {
                    response = getStub(channel).queryMaintenanceRecordList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryMaintenanceRecordList");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 保养结果查询
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, RecordDetailResp> queryMaintenanceItemList(CallBack<RecordDetailResp> callBack) {
        return new GrpcAsyncTask<String, Void, RecordDetailResp>(callBack) {
            @Override
            protected RecordDetailResp doRequestData(ManagedChannel channel) {
                RecordDetailReq message = RecordDetailReq.newBuilder()
                        .build();
                Logger.d(message);
                RecordDetailResp response = null;
                try {
                    response = getStub(channel).queryMaintenanceItemList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryMaintenanceItemList");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

}
